import 'package:flutter/material.dart';
import 'package:flutter_architecture_samples_inherited_widget/localization.dart';
import 'package:flutter_architecture_samples_inherited_widget/state_container.dart';
import 'package:flutter_architecture_samples_inherited_widget/widgets/extra_actions_button.dart';
import 'package:flutter_architecture_samples_inherited_widget/widgets/filter_button.dart';
import 'package:todos_app_core/todos_app_core.dart';
import '../models.dart';
import '../widgets/stats_counter.dart';
import '../widgets/todo_list.dart';

class HomeScreen extends StatefulWidget {
  const HomeScreen({Key? key}) : super(key: key);

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  AppTab activeTab = AppTab.todos;
  @override
  Widget build(BuildContext context) {
    final container = StateContainer.of(context);
    final state = container.state;
    return Scaffold(
      appBar: AppBar(
        title: Text(InheritedWidgetLocalizations.of(context)!.appTitle),
        actions: [
          FilterButton(
              onSelected: container.updateFilter,
              activeFilter: state!.activeFilter,
              isActive: activeTab == AppTab.todos),
          ExtraActionsButton(
            onSelected: (action) {
              if (action == ExtraAction.toggleAllComplete) {
                container.toggleAll();
              } else if (action == ExtraAction.clearCompleted) {
                container.clearCompleted();
              }
            },
            allComplete: state!.allComplete,
            hasCompletedTodos: state!.hasCompletedTodos,
          )
        ],
      ),
      body: activeTab == AppTab.todos ? TodoList() : StatsCounter(),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          Navigator.pushNamed(context, ArchSampleRoutes.addTodo);
        },
        child: Icon(Icons.add),
      ),
    );
  }
}
